package com.jason.demo.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/redis")
@Api(tags = {"redis 测试API"})
public class RedisController {
	
	@Autowired
	RedisTemplate<String, String> redisTemplate;
	
	@PostMapping("/set")
	@ApiOperation("设置type is string : key-value")
	public Map<String, Object> setKeyValue(@RequestParam("key") String key, @RequestParam("value") String value) {
		redisTemplate.opsForValue().set(key, value);
		return resultMap(key, value);
	}
	
	@GetMapping("/get/{key}")
	@ApiOperation("根据key获取value")
	public Map<String, Object> valueForKey(@PathVariable("key") String key) {
		return resultMap(key, redisTemplate.opsForValue().get(key));
	}
	
	private Map<String, Object> resultMap(String key, Object object) {
		Map<String, Object> map = new HashMap<>();
		map.put("key", key);
		map.put("value", object);
		return map;
	}
}
